function [a,P] = SelectActionFromPolicy(P,S,mode, policy)

global log_id;
global compare_policies;
global policies;
global system_mode;
global total_reward;
global num_passes;
global num_players_r;
global num_players_b;

if(P{2}=='r')
    num_players = num_players_r;
    num_opponents = num_players_b;
else
    num_players = num_players_b;
    num_opponents = num_players_r;
end

P_num = P{3};

if(PlayerInPossession(P,S,mode)) % choose between Dribble and PassBall(k)
    range = [(1:P_num), (P_num+2:num_players+1)];
    %range = 1:(num_players+1);
    [max_action, max_reward] = FindBestAction(P, S, mode,range,policy);
else
    if(TeamInPossession(P,S,mode)) % choose GetOpen and (perhaps) stand still
        range = [2 + num_players, 1 + P_num];
        [max_action, max_reward] = FindBestAction(P, S, mode,range,policy);
        %[a,P] = GetOpen(P,S,mode);
        %SaveMemory(P,true,2+num_players,0);
    elseif(OpponentInPossession(P,S,mode)) % choose between GoToBall, BlockPass(k)
        range = (3+num_players):(3+num_players+num_opponents);
        [max_action, max_reward] = FindBestAction(P,S,mode,range, policy);
    else % choose between GetOpen and GoToBall and BlockPass
        %if(IsClosest(P,S,mode))
        %    max_action = 3 + num_players;
            %[a,P] = GoToBall(P,S,mode);
            %SaveMemory(P,true,3+num_players,0);
        %else
            %range = [2+num_players, 4+num_players:3+num_players+num_opponents];
            range = [2+num_players:3+num_players+num_opponents];

            [max_action, max_reward] = FindBestAction(P,S,mode,range, policy);
        %end
    end
end

if(max_action==1)                       % Dribble
    [a, P] = Dribble(P);
    SaveMemory(P,false,max_action,0);
elseif(max_action>=2 && max_action <=(1+num_players)) % PassBall
    k = max_action-1;                % identify the teammate to pass to
    [a,P] = PassBall(P,S,mode,k);
    SaveMemory(P,true,max_action,0);
    
    if (P{2} == 'r' && P_num ~= k) % update pass counter if learning agent chose to pass 
        num_passes = num_passes + 1;
        %disp(sprintf('Player %s passed to player %s', num2str(P_num), num2str(k)));
    end
elseif(max_action == (2+num_players))   % GetOpen
    [a,P] = GetOpen(P,S,mode);
    SaveMemory(P,true,max_action,0);
elseif(max_action == (3 + num_players)) % GoToBall
    [a,P] = GoToBall(P,S,mode);
    SaveMemory(P,true,max_action,0);
else % BlockPass
    k = max_action - (3 + num_players);
    [a,P] = BlockPass(P,S,mode,k);
    SaveMemory(P,true,max_action,0);
end

if(compare_policies)
    
    line = sprintf('Player %d |',P_num);
    for policy_num = 1:numel(policies)
        [action, value] = SamplePolicy(P,S,mode, policies{policy_num});
        line = strcat(line,sprintf(' %s |', ActionToString(action, value, num_players_r, num_players_b)));
    end
    line = strcat(line,'\n');
    fprintf(log_id, line);
end

if(strcmp(system_mode, 'follow'))
    
% get the reward for the last action
old_state_value = LoadValue(P);
current_state_value = GetReward(P,S,mode);
SaveValue(P,current_state_value);

reward =  current_state_value - old_state_value;
total_reward = total_reward + reward;
    
end

end
